﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class WXAvatarManager : MonoBehaviour {

    public static WXAvatarManager _instance;
    public Dictionary<int,Texture> wxAvatarDic = new Dictionary<int,Texture>();//玩家的微信头像
    //public static UITexture myWXAvatar;//我自己的微信头像

	void Start () {
        _instance = this;
        DontDestroyOnLoad(gameObject);
	}
	
	public  void  GetWxAvatar(int uid,UITexture uiTexture,string url){ 
        Texture texture = null;     
        if(wxAvatarDic.TryGetValue(uid,out texture)){
            uiTexture.mainTexture = wxAvatarDic[uid];
        }
        else
        {
            StartCoroutine(GetWWWFace(uid,url, uiTexture));
            
        }        
    }


    IEnumerator GetWWWFace(int uid ,string url,UITexture uiTexture)
    {
        if (url != "")
        {
            Debug.Log("下载微信头像");
            WWW www = new WWW(url);

            yield return www;
            if (www.isDone)
            {
                if (www.error == null) { }
                uiTexture.mainTexture = www.texture;
                wxAvatarDic.Add(uid, uiTexture.mainTexture);
                Debug.Log("保存了微信头像" + uiTexture);
                Debug.Log("uiTexture = " + uiTexture);               
            }
            else
            {

            }
            www.Dispose();
        }

    }
}
